package org.rsbot.gui.actions;

import org.rsbot.gui.ConsolePanel;
import org.rsbot.util.GlobalConfiguration;
import org.rsbot.util.GlobalConfiguration.OperatingSystem;
import java.lang.reflect.Method;

/**
 * @author Sorcermus
 */
public class GoToFaceCmd1 extends BaseAction1 {

    private static final long serialVersionUID = 4454656573454372123L;

    public GoToFaceCmd1(ConsolePanel frame) {
        super(frame);
    }

    public void actionPerformed(java.awt.event.ActionEvent e) {
        openURL("http://www.facebook.com/pages/Runedevinfo/144013358963281");
    }

    /**
     * This method initializes url use
     * opening the users web browser to link to a given url
     *
     */
    public void openURL(final String url) {
        final OperatingSystem os = GlobalConfiguration.getCurrentOperatingSystem();
        try {
            if (os == OperatingSystem.MAC) {
                final Class<?> fileMgr = Class.forName("com.apple.eio.FileManager");
                final Method openURL = fileMgr.getDeclaredMethod("openURL",
                        new Class[]{String.class});
                openURL.invoke(null, url);
            } else if (os == OperatingSystem.WINDOWS) {
                Runtime.getRuntime().exec("rundll32 url.dll,FileProtocolHandler " + url);
            } else { // assume Unix or Linux
                final String[] browsers = {"firefox", "opera", "konqueror", "epiphany", "mozilla", "netscape"};
                String browser = null;
                for (int count = 0; (count < browsers.length) && (browser == null); count++) {
                    if (Runtime.getRuntime().exec(new String[]{"which", browsers[count]}).waitFor() == 0) {
                        browser = browsers[count];
                    }
                }
                if (browser == null) {
                    throw new Exception("Could not find web browser");
                } else {
                    Runtime.getRuntime().exec(new String[]{browser, url});
                }
            }
        } catch (final Exception e) {
        }
    }
}
